package com.example.mydatabaseapp.data.infrastructure.repository;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import com.example.mydatabaseapp.data.infrastructure.context.DatabaseOpenHelper;
import com.example.mydatabaseapp.data.domain.ShaftPosition;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ShaftPositionRepository implements IRepositoryBase<ShaftPosition>{
	private SQLiteDatabase database;
	
	public ShaftPositionRepository(Context context) {
		this.database = (new DatabaseOpenHelper(context)).getWritableDatabase();
	}
	
	public void Drop() {
		this.database.execSQL("DROP TABLE IF EXISTS " + DatabaseOpenHelper.SHAFT_POSITION_TABLE);
	}

	@Override
	public ShaftPosition find(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ShaftPosition> findAll() {
		Cursor cursor = this.database.rawQuery("SELECT * FROM "+DatabaseOpenHelper.SHAFT_POSITION_TABLE, null);
		if(cursor == null)
			return null;
		
		List<ShaftPosition> collection = new ArrayList<ShaftPosition>();
		
		if(cursor.moveToFirst()){
			do{
				ShaftPosition item = new ShaftPosition();
				item.setNumber(cursor.getString(0));
				item.setName(cursor.getString(1));
				item.setLatitude(cursor.getString(2));
				item.setLongitude(cursor.getString(3));
				collection.add(item);
			}while(cursor.moveToNext());		
		}
		
		if(cursor.isClosed() == false)
			cursor.close();		
				
		return collection;
	}

	@Override
	public void delete(ShaftPosition entity) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void add(ShaftPosition entity) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("NUMBER", entity.getNumber());
		contentValues.put("NAME", entity.getName());
		contentValues.put("LATITUDE", entity.getLatitude());
		contentValues.put("LONGITUDE", entity.getLongitude());
		
		this.database.insert(DatabaseOpenHelper.SHAFT_POSITION_TABLE, null, contentValues);		
	}

	@Override
	public void update(ShaftPosition entity) {
		// TODO Auto-generated method stub
		
	}
}
